﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wise.Entities
{
    static class _Extersion_For_Column
    {
        public static dynamic Get_Data<TPara>(string key, Func<TPara, dynamic> onExpired, TPara para)
        {
            if (MyDbConfig.Current == null)
                return false;
            if (!MyDbConfig.Current.MyCache.ContainsKey(key))
            {
                dynamic dd = onExpired(para);
                if (!MyDbConfig.Current.MyCache.ContainsKey(key))
                {
                    MyDbConfig.Current.MyCache.Add(key, dd);
                }
            }
            return MyDbConfig.Current.MyCache[key];
        }

        public static bool NeedEncrypt(this 表映射 _table,string columnName)
        {
            return Get_Data<Tuple<表映射,string>>(
                $"NeedEncrypt_{_table.Id}_{_table.源表名}_{_table.目标表名}_{columnName}"
                , _NeedEncrypt,new Tuple<Entities.表映射, string> (_table,columnName));
        }

        static dynamic _NeedEncrypt(Tuple<Entities.表映射, string> para)
        {
            if (para.Item1.字段映射 == null || para.Item1.字段映射.Count == 0)
                return false;
            return para.Item1.字段映射
                .Where(x => x.加密数据)
                .Any(x => string.Equals(para.Item2, x.新数据库字段, StringComparison.CurrentCultureIgnoreCase));
        }
    }
}
